package com.jrx.anytxn.param.utils;

import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.param.enums.BizTypeEnum;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;

/**
 * @author user
 */
public class BizTypeProducts {

    private static EnumMap<BizTypeEnum, List<String>> bizTypeProductMap;
    public static final List<String> PRODUCT_XAH = Arrays.asList("XIBXAH000001", "XIBXAH000002", "XIBXAH000003");
    public static final String PRODUCT_XAF = "XIBXAF000001";
    public static final String PRODUCT_XYJY = "XIBXYJY00001";
    public static final String XAH_PRODUCT = "XIBXAH000001";
    public static final String XAJ_PRODUCT = "XIBXAJ000001";
    private static List<String> PRODUCT_XAH_SERIES;
    private static List<String> PRODUCT_XAJ_SERIES;
    private static List<String> PRODUCT_XAF_SERIES;
    private static List<String> PRODUCT_XAH_AND_XAJ_SERIES;
    private static List<String> PRODUCT_XAH_CREDIT_PRODUCT;

    static {
        bizTypeProductMap = new EnumMap<>(BizTypeEnum.class);
        bizTypeProductMap.put(BizTypeEnum.BIZ_TYPE_XAH, PRODUCT_XAH);
        bizTypeProductMap.put(BizTypeEnum.BIZ_TYPE_XAF, Arrays.asList(PRODUCT_XAF));
        bizTypeProductMap.put(BizTypeEnum.BIZ_TYPE_XYJY, Arrays.asList(PRODUCT_XYJY));
        bizTypeProductMap.put(BizTypeEnum.BIZ_TYPE_XAJ, Arrays.asList(XAJ_PRODUCT));
        PRODUCT_XAH_SERIES = new ArrayList<>(10);
        PRODUCT_XAH_SERIES.add(PRODUCT_XYJY);
        PRODUCT_XAH_SERIES.addAll(PRODUCT_XAH);
        PRODUCT_XAJ_SERIES = new ArrayList<>(10);
        PRODUCT_XAJ_SERIES.add(XAJ_PRODUCT);
        PRODUCT_XAF_SERIES = new ArrayList<>(10);
        PRODUCT_XAF_SERIES.add(PRODUCT_XAF);
        PRODUCT_XAH_AND_XAJ_SERIES = new ArrayList<>(20);
        PRODUCT_XAH_AND_XAJ_SERIES.addAll(PRODUCT_XAH_SERIES);
        PRODUCT_XAH_AND_XAJ_SERIES.addAll(PRODUCT_XAJ_SERIES);
        PRODUCT_XAH_CREDIT_PRODUCT = new ArrayList<>(10);
        PRODUCT_XAH_CREDIT_PRODUCT.add(PRODUCT_XAH.get(0));
        PRODUCT_XAH_CREDIT_PRODUCT.add(PRODUCT_XYJY);
    }

    public static List<String> getByBizType(BizTypeEnum bizTypeEnum) {
        List<String> products = bizTypeProductMap.get(bizTypeEnum);

        if (CollectionUtils.isEmpty(products)) {
            throw new RuntimeException(bizTypeEnum.getCode() + "对应的产品信息不存在");
        }
        return products;
    }


    public static BizTypeEnum getBizType(String productCode) {
        if (StringUtils.isBlank(productCode)) {
            throw new RuntimeException(productCode + "产品码为空");
        }

        for (Map.Entry<BizTypeEnum, List<String>> enumMap : bizTypeProductMap.entrySet()) {
            List<String> products = enumMap.getValue();
            if (products.contains(productCode)) {
                return enumMap.getKey();
            }
        }

        throw new RuntimeException(productCode + "对应的产品信息不存在");
    }

    public static List<String> getXAHSeriesProducts() {
        if (CollectionUtils.isEmpty(PRODUCT_XAH_SERIES)) {
            new RuntimeException("兴安花系列产品初始化异常");
        }
        return PRODUCT_XAH_SERIES;
    }

    public static List<String> getXAJSeriesProducts() {
        if (CollectionUtils.isEmpty(PRODUCT_XAJ_SERIES)) {
            new RuntimeException("兴安借系列产品初始化异常");
        }
        return PRODUCT_XAJ_SERIES;
    }

    public static List<String> getXAHAndXAJSeriesProducts() {
        if (CollectionUtils.isEmpty(PRODUCT_XAH_AND_XAJ_SERIES)) {
            new RuntimeException("兴安花、兴安借系列产品初始化异常");
        }
        return PRODUCT_XAH_AND_XAJ_SERIES;
    }

    /**
     * 通过产品编号判断同系列有哪些产品编号
     * @param productId
     * @return
     */
    public static List<String> getSeriesProductIds(String productId) {
        if (StringUtils.isNotBlank(productId)) {
            if (PRODUCT_XAH_SERIES.contains(productId)) {
                return PRODUCT_XAH_SERIES;
            }else if(PRODUCT_XAJ_SERIES.contains(productId)){
                return PRODUCT_XAJ_SERIES;
            }else {
                return Collections.singletonList(productId);
            }
        } else {
            return PRODUCT_XAH_SERIES;
        }
    }

    //兴安花系列贷款产品-----用于汇总贷款金额
    public static List<String> getXAHCreditProducts() {
        if (CollectionUtils.isEmpty(PRODUCT_XAH_CREDIT_PRODUCT)) {
            new RuntimeException("兴安花系列产品初始化异常");
        }
        return PRODUCT_XAH_CREDIT_PRODUCT;
    }


    /**
     * 获取代表的产品编号
     * @param productId
     * @return
     */
    public static String getSymbolProductId(String productId) {
        if (PRODUCT_XAH_SERIES.contains(productId)) {
            return XAH_PRODUCT;
        } else if (PRODUCT_XAJ_SERIES.contains(productId)) {
            return XAJ_PRODUCT;
        } else if (PRODUCT_XAF_SERIES.contains(productId)) {
            return PRODUCT_XAF;
        }
        return XAH_PRODUCT;
    }

}
